package com.news.controller;

import entity.Attribute;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.IUserService;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
     IUserService ius;
    @RequestMapping("/login")
    public String m1(User user, HttpSession session) {
        System.out.println(user);
        User user1 = ius.userLogin(user);
        if (user != null) {
            session.setAttribute("user", user1);
            System.out.println("ces");
            return "redirect:/news/index";
        }
        return "login";
    }
    // 更新用户信息有就修改没有就添加
    @RequestMapping("/update")
    public void m3(String id, Attribute attr, HttpServletResponse res) throws IllegalAccessException, IOException {
        ius.updateAttribute(id, attr);
        res.getWriter().print("ok");
        res.getWriter().close();
    }

    // 删除用户属性
    @RequestMapping("/deleteAttr")
    public void m4(User user, HttpServletResponse res) throws IOException, IllegalAccessException {
        List<Attribute> attributes = user.getAttributes();
        for (Attribute attr : attributes) {
            ius.deleteAttribute(user.getId(),attr.getColumn());
        }
        res.getWriter().print("ok");
        res.getWriter().close();
    }
    // 用户id删除
    @RequestMapping("/deleteid")
    public void m5(String id, HttpServletResponse res) throws IOException {
        System.out.println(id);
        ius.deleteUserById(id);
        res.getWriter().print("ok");
        res.getWriter().close();
    }
    // 按照attribute删除
    @RequestMapping("/deleteAttribute")
    public void m6(User user, HttpServletResponse res) throws IOException {
        List<Attribute> attr = user.getAttributes();
        Attribute[] objects = (Attribute[]) attr.toArray();
        ius.deleteUserByAttribute(objects);
        res.getWriter().print("ok");
        res.getWriter().close();
    }
    // 用户id查询接口
    @RequestMapping("/queryId")
    @ResponseBody
    public Map<String,Object> m7(String id, HttpServletResponse res) throws IOException {
        Map<String,Object> map=new HashMap<String,Object>();
        User user = ius.queryUserById(id);
        map.put("id",user.getId());
        map.put("name",user.getName());
        map.put("type",user.getType());
        for (Attribute attr : user.getAttributes()) {
            map.put(attr.getColumn(),attr.getValue());
        }
        return map;
    }
    // 分页查询
    @RequestMapping("/query/{pageNum}/{pageSize}")
    public String m8(Model model, @PathVariable("pageNum")int pageNum, @PathVariable("pageSize") int pageSize) throws IOException {
        ius.queryAll();
        List<User> users = ius.queryByAttributes(pageNum , pageSize);
        System.out.println(users);
        model.addAttribute("user",users);
        return "picture-list";
    }
}
